/******************************************************************************
 * * desktop_switch_off.c - implementation of switch off functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "desktop_global.h"


/**
 * Inform framework to switch off if user selects snooze when alarm during power off
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Alarm_Poweroff_Snooze_Ind(VOID)
{
    Framework_Set_Phone_Ready_Status(FALSE);
    Hal_Power_Off(HAL_TYPE_POWER_OFF);
}


/**
 * Callback function for handling key pressed on Ask_About_Power_On interface
 * param (in) Obj: lv_obj_t *
 * param (in) Key_Sta: UI_KEY_STATUS
 * param (in) Key_Val: UI_KEY_VALUE
 * return  UINT8
 */
UINT8 Framework_Ask_About_Power_On_Key_Cb(lv_obj_t *Obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val)
{
    if (KEY_RELEASED == Key_Sta)
    {
        switch (Key_Val)
        {
        case KEY_OK: // Yes
#if 0
            Hal_Power_Off(HAL_TYPE_REBOOT);
#else
            g_FrameworkMng.RebootProc = FRAMEWORK_REBOOT_ONGOING;
            Framework_Reboot();
#endif
            break;

        case KEY_SOFTRIGHT: // No
        case KEY_END:
            Framework_Set_Phone_Ready_Status(FALSE);
            Hal_Power_Off(HAL_TYPE_POWER_OFF);
            break;

        default:
            break;
        }
    }

    return(1);
}


/**
 * Create Ask_About_Power_On interface
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Create_Ask_About_Power_On(VOID)
{
    UI_Normal_NoTitle_Desc_t *NormalNoTitleDesc;
    Nav_Func_List_t          FuncList;

    NormalNoTitleDesc = (UI_Normal_NoTitle_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_NoTitle_Desc_t));
    memset(NormalNoTitleDesc, 0, sizeof(UI_Normal_NoTitle_Desc_t));

    NormalNoTitleDesc->ContenCont.ContLabel.TxtId      = PHONE_TEXT_ID_SWITCH_ON;
    NormalNoTitleDesc->ContenCont.ContLabel.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    NormalNoTitleDesc->ContenCont.ContLabel.LabelAlign = LV_ALIGN_IN_TOP_LEFT;

    NormalNoTitleDesc->ButtonBar.ButtonL.Valid = FALSE;
    NormalNoTitleDesc->ButtonBar.ButtonM.Valid = TRUE;
    NormalNoTitleDesc->ButtonBar.ButtonM.TxtId = PHONE_TEXT_ID_YES;
    NormalNoTitleDesc->ButtonBar.ButtonR.Valid = TRUE;
    NormalNoTitleDesc->ButtonBar.ButtonR.TxtId = PHONE_TEXT_ID_NO;
    NormalNoTitleDesc->KeyFunc                 = Framework_Ask_About_Power_On_Key_Cb;

    memset(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow    = UI_Normal_NoTitle_Create;
    FuncList.OnDestroy = Framework_Interface_OnDestroy;

    Nav_Forward(ACT_ID_ANY, ACT_ID_ASK_ABOUT_POWER_ON, &FuncList, NormalNoTitleDesc);
}


/**
 * Inform framework to ask user whether to switch on if user selects to stop alarm during power off
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Poweroff_Stop_Alarm_Ind(VOID)
{
    Framework_Create_Ask_About_Power_On();
}


/**
 * Callback function for additional actions after Creating Shutdown_Animation interface
 * param (in) Ctrl: VOID *
 * return  VOID
 */
VOID Framework_Shutdown_Animation_OnCreate(VOID *Ctrl)
{
    lv_label_set_text(((UI_FullScreen_Label_Only_Desc_t *)Ctrl)->ContLabel.Label, "ASR");
}


/**
 * Create Shutdown_Animation interface
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Create_Shutdown_Animation(VOID)
{
    UI_FullScreen_Label_Only_Desc_t *FullScrnLblOnlyDesc;
    Nav_Func_List_t                 FuncList;

    FullScrnLblOnlyDesc = (UI_FullScreen_Label_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_FullScreen_Label_Only_Desc_t));
    memset(FullScrnLblOnlyDesc, 0, sizeof(UI_FullScreen_Label_Only_Desc_t));

    FullScrnLblOnlyDesc->ContLabel.TxtId = LV_LANG_TXT_ID_NONE;
    FullScrnLblOnlyDesc->ContLabel.TxtFont = LV_THEME_SIMPLE_FONT_BIG;
    FullScrnLblOnlyDesc->ContLabel.LabelAlign = LV_ALIGN_IN_LEFT_MID;
    FullScrnLblOnlyDesc->ContLabel.TxtAlign   = LV_LABEL_ALIGN_CENTER;

    memset(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow   = UI_FullScreen_Label_Only_Create;
    FuncList.OnCreate = Framework_Shutdown_Animation_OnCreate;

    Nav_Forward(ACT_ID_ANY, ACT_ID_SHUTDOWN_ANIMATION, &FuncList, FullScrnLblOnlyDesc);
}


/**
 * Action if pressing OK key on volume container in Switch_Off interface
 * param (in) Btn: lv_obj_t *
 * param (in) Event: lv_event_t
 * return  VOID
 */
VOID Framework_Switch_Off_Press_Ok_On_Vol_Cont_Cb(lv_obj_t *Cont, lv_event_t Event)
{
    if (LV_EVENT_CLICKED != Event)
    {
        return;
    }

    VOID                     *Param     = Nav_Get_Param(ACT_ID_SWITCH_OFF);
    UI_Container_List_Desc_t *ContList  = &(((UI_Normal_Container_List_Desc_t *)Param)->ContList);
    UI_Bar_Desc_t            *VolSlider = &(ContList->ContainerList[0].u.Bar);

    VolSlider->Value = lv_slider_get_value(VolSlider->Bar);
    Setting_Set_Ring_Volume_Req((HAL_SPEAKER_GAIN)(VolSlider->Value));
    if(TRUE == Audio_Player_Is_Running())
    {
        Hal_Audio_SetVolume((UINT16)VolSlider->Value);
    }
    Nav_Back(ACT_ID_ANY);
}


/**
 * callback function for shutdown
 * param (in) Task: lv_task_t *
 * return  VOID
 */
VOID Framework_Shutdown_Cb(lv_task_t * Task)
{
    Framework_Set_Phone_Ready_Status(FALSE);
    Hal_Power_Off(HAL_TYPE_POWER_OFF);
}


/**
 * Switch off
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Switch_Off_Handle(VOID)
{
    If_Setting_Play_Tone_t *SettingPlayTone;

    // stop the keypad tone
    SettingPlayTone = (If_Setting_Play_Tone_t *)Hal_Mem_Alloc(sizeof(If_Setting_Play_Tone_t));
    SettingPlayTone->PlayCtrl = IF_SETTING_PLAY_TONS_STOP;
    SettingPlayTone->ToneType = IF_SETTING_TONS_TYPE_KEYPAD;
    Setting_Play_Tone_Req(SettingPlayTone);

    Framework_Create_Shutdown_Animation();
    lowbattery_handle_destroy();
#if USE_LV_BLUETOOTH != 0
    extern VOID bt_power_off(VOID);
    bt_power_off();
#endif
    lv_task_t *t = lv_task_create(Framework_Shutdown_Cb, 10, LV_TASK_PRIO_HIGH, NULL);
    lv_task_once(t);
}


/**
 * Action if pressing switch off button on Switch_Off interface
 * param (in) Btn: lv_obj_t *
 * param (in) Event: lv_event_t
 * return  VOID
 */
VOID Framework_Switch_Off_Btn_Switch_Off_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if (LV_EVENT_CLICKED != Event)
    {
        return;
    }

    Framework_Switch_Off_Handle();
}


/**
 * Callback function for handling key pressed on Switch_Off interface
 * param (in) Obj: lv_obj_t *
 * param (in) Key_Sta: UI_KEY_STATUS
 * param (in) Key_Val: UI_KEY_VALUE
 * return  UINT8
 */
UINT8 Framework_Switch_Off_Key_Cb(lv_obj_t *Obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val)
{
    VOID                     *Param;
    UI_Container_List_Desc_t *ContList;
    UINT8                    Ret = 1;

    if (KEY_RELEASED == Key_Sta)
    {
        Param    = Nav_Get_Param(ACT_ID_SWITCH_OFF);
        ContList = &(((UI_Normal_Container_List_Desc_t *)Param)->ContList);

        switch (Key_Val)
        {
        case KEY_SOFTLEFT:
            Framework_Keyguard_Or_Unlock();
            break;

        case KEY_SOFTRIGHT:
        case KEY_END:
            Nav_Back(ACT_ID_ANY);
            break;

        case KEY_UP:
        case KEY_DOWN:
            if (0 == ContList->Index)
            {
                ContList->Index = 1;
            }
            else
            {
                ContList->Index = 0;
            }
            Ret = 0;
            break;

        case KEY_LEFT:
        case KEY_RIGHT:
            Ret = 1;
            break;

        case KEY_OK:
            Ret = 0;
            break;

        default:
            break;
        } /* switch */
    }

    return(Ret);
} /* Framework_Switch_Off_Key_Cb */


/**
 * Callback function for saving state on Contacts_Ring_Tone interface before forward to another activity
 * param (in) Ctrl: VOID *
 * return  VOID
 */
VOID Framework_Switch_Off_OnSaveState(VOID *Ctrl)
{
    UI_Normal_Container_List_Desc_t *Interface = (UI_Normal_Container_List_Desc_t *)Ctrl;
    UI_Bar_Desc_t                   *ValSlider = &(Interface->ContList.ContainerList[0].u.Bar);

    ValSlider->Value = lv_slider_get_value(ValSlider->Bar);
}


/**
 * Callback function for destroying Switch_Off interface which will be popped from stack
 * param (in) Ctrl: VOID *
 * return  VOID
 */
VOID Framework_Switch_Off_OnDestroy(VOID *Ctrl)
{
    Hal_Mem_Free(((UI_Normal_Container_List_Desc_t *)Ctrl)->ContList.ContainerList);
    Hal_Mem_Free(Ctrl);
}


/**
 * Create Switch_Off interface
 * param (in) VOID
 * return  VOID
 */
VOID Framework_Create_Switch_Off(VOID)
{
    UI_Normal_Container_List_Desc_t *SwitchOff;
    Nav_Func_List_t                 FuncList;
    UI_Container_Base_Desc_t        *ContList;
    UINT32                          Len;

    SwitchOff = (UI_Normal_Container_List_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_Container_List_Desc_t));
    Hal_Mem_Set(SwitchOff, 0, sizeof(UI_Normal_Container_List_Desc_t));

    SwitchOff->ContList.ListContainerCnt = 2;
    Len                                  = sizeof(UI_Container_Base_Desc_t) * SwitchOff->ContList.ListContainerCnt;
    SwitchOff->ContList.ContainerList    = (UI_Container_Base_Desc_t *)Hal_Mem_Alloc(Len);
    Hal_Mem_Set(SwitchOff->ContList.ContainerList, 0, Len);
    ContList = SwitchOff->ContList.ContainerList;

    ContList[0].Valid            = TRUE;
    ContList[0].Label.TxtId      = PHONE_TEXT_ID_VOLUME;
    ContList[0].Label.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    ContList[0].Label.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    ContList[0].Choise           = UI_BASE_OBJ_BAR;
    ContList[0].u.Bar.Range      = HAL_AUDIO_SPK_LEVEL_MAX;
    ContList[0].u.Bar.Value      = Setting_Get_Ring_Volume_Req();
    ContList[0].u.Bar.BarAlign   = LV_ALIGN_IN_BOTTOM_LEFT;
    ContList[0].ReleaseFunc      = Framework_Switch_Off_Press_Ok_On_Vol_Cont_Cb;

    ContList[1].Valid            = TRUE;
    ContList[1].Label.TxtId      = PHONE_TEXT_ID_SWITCH_OFF;
    ContList[1].Label.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    ContList[1].Label.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    ContList[1].Choise           = UI_BASE_OBJ_NULL;
    ContList[1].ReleaseFunc      = Framework_Switch_Off_Btn_Switch_Off_Cb;

    SwitchOff->ButtonBar.ButtonL.Valid = TRUE;
    SwitchOff->ButtonBar.ButtonL.TxtId = PHONE_TEXT_ID_LOCK;
    SwitchOff->ButtonBar.ButtonM.Valid = TRUE;
    SwitchOff->ButtonBar.ButtonM.TxtId = PHONE_TEXT_ID_OK;
    SwitchOff->ButtonBar.ButtonR.Valid = TRUE;
    SwitchOff->ButtonBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;
    SwitchOff->KeyFunc                 = Framework_Switch_Off_Key_Cb;

    memset(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow      = UI_Normal_Switch_Off_Create;
    FuncList.OnSaveState = Framework_Switch_Off_OnSaveState;
    FuncList.OnDestroy   = Framework_Switch_Off_OnDestroy;

    Nav_Forward(ACT_ID_ANY, ACT_ID_SWITCH_OFF, &FuncList, SwitchOff);
} /* Framework_Create_Switch_Off */
